using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System.Json;
using System.IO;
using System.Globalization;

namespace Tsanie.Common
{
    static class JsonExtends
    {
        public static string ToJsonString(this JsonValue json)
        {
            StringWriter w = new StringWriter();
            ToJsonStringInternal(json, w);
            return w.ToString();
        }

        static string GetFormattedString(JsonValue json)
        {
            JsonType jsonType = json.JsonType;
            if (jsonType != JsonType.String)
            {
                if (jsonType != JsonType.Number)
                {
                    throw new InvalidOperationException();
                }
                return string.Format("{0:G}", json);
            }
            else
            {
                return (string)json;
            }
        }

        static string EscapeString(string src)
        {
            if (src == null)
            {
                return null;
            }
            for (int i = 0; i < src.Length; i++)
            {
                if (src[i] == '"' || src[i] == '\\')
                {
                    StringBuilder stringBuilder = new StringBuilder();
                    if (i > 0)
                    {
                        stringBuilder.Append(src, 0, i);
                    }
                    return DoEscapeString(stringBuilder, src, i);
                }
            }
            return src;
        }

        static string DoEscapeString(StringBuilder sb, string src, int cur)
        {
            int num = cur;
            for (int i = cur; i < src.Length; i++)
            {
                if (src[i] == '"' || src[i] == '\\')
                {
                    sb.Append(src, num, i - num);
                    sb.Append('\\');
                    sb.Append(src[i++]);
                    num = i;
                }
            }
            sb.Append(src, num, src.Length - num);
            return sb.ToString();
        }

        static void ToJsonStringInternal(JsonValue json, TextWriter w)
        {
            switch (json.JsonType)
            {
                case JsonType.String:
                    w.Write('"');
                    w.Write(EscapeString(GetFormattedString(json)));
                    w.Write('"');
                    return;
                case JsonType.Object:
                    {
                        w.Write('{');
                        bool flag = false;
                        foreach (KeyValuePair<string, JsonValue> current in (JsonObject)json)
                        {
                            if (flag)
                            {
                                w.Write(',');
                            }
                            w.Write('"');
                            w.Write(EscapeString(current.Key));
                            w.Write("\":");
                            if (current.Value == null)
                            {
                                w.Write("null");
                            }
                            else
                            {
                                ToJsonStringInternal(current.Value, w);
                            }
                            flag = true;
                        }
                        w.Write('}');
                        return;
                    }
                case JsonType.Array:
                    {
                        w.Write('[');
                        bool flag = false;
                        foreach (JsonValue current in (IEnumerable<JsonValue>)((JsonArray)json))
                        {
                            if (flag)
                            {
                                w.Write(',');
                            }
                            if (current != null)
                            {
                                ToJsonStringInternal(current, w);
                            }
                            else
                            {
                                w.Write("null");
                            }
                            flag = true;
                        }
                        w.Write(']');
                        return;
                    }
                case JsonType.Boolean:
                    w.Write((!json) ? "false" : "true");
                    return;
            }
            w.Write(GetFormattedString(json));
        }
    }
}